<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of candidate_requirements_controller
 *
 * @author wilson
 */
class CandidateRequirementscontroller extends AppController {

    //put your code here
    var $name = "CandidateRequirements";

    function index($id = null) {
        $this->layout = "ajax";
        $this->loadRequirements($id);
        $requerimientos = $this->CandidateRequirement->find('all', array('recursive' => 0, 'conditions' => array('CandidateRequirement.candidate_id' => $id, 'CandidateRequirement.claim_id' => 0), 'fields' => array('CandidateRequirement.id', 'Requirement.texto', 'CandidateRequirement.calificacion')));
        $this->set('requisitos', $requerimientos);
    }

    function edit($id = null) {
        $this->set('est', $this->Session->read('estado'));
        if (empty($this->data)) {
            App::import('Model', 'RequirementFault');
            $ava = new RequirementFault();
            //$this->CandidateRequirement->recursive=0;
            $this->data = $this->CandidateRequirement->find('first', array('conditions' => array('CandidateRequirement.id' => $id), 'fields' => array('CandidateRequirement.id', 'CandidateRequirement.calificacion', 'CandidateRequirement.candidate_id', 'CandidateRequirement.requirement_id', 'Requirement.texto', 'Candidate.1er_nombre', 'Candidate.1er_apellido')));

            // echo "id del reqAsociado al can $id";
            //echo "reque id".$this->data['CandidateRequirement']['requirement_id'];
            $ava->recursive = -1;
            $this->set('chequeados', $this->CandidateRequirement->CandidateRequirementsRequirementFault->findAllByCandidateRequirementId($id));
            $this->set('condiciones', $ava->find('all', array('conditions' => array('RequirementFault.requirement_id' => $this->data['CandidateRequirement']['requirement_id']), 'fields' => array('RequirementFault.id', 'RequirementFault.nombre', 'RequirementFault.tipo'), 'order' => array('RequirementFault.tipo ASC'))));
        } else {
            $datos = $this->params['form'];
            $this->CandidateRequirement->create();
            if ($this->CandidateRequirement->save($this->data)) {
                $ide = $this->data['CandidateRequirement']['id'];
                $idr = $this->data['CandidateRequirement']['candidate_id'];

                $this->CandidateRequirement->CandidateRequirementsRequirementFault->recursive = -1;
                $this->CandidateRequirement->CandidateRequirementsRequirementFault->deleteAll(array('CandidateRequirementsRequirementFault.candidate_requirement_id' => $ide));
                $flag = 0;
                foreach ($datos['checked'] as $value) {

                    if ($value != 0) {
                        $flag = 1;
                        $this->CandidateRequirement->CandidateRequirementsRequirementFault->query("INSERT INTO candidate_requirements_requirement_faults (requirement_fault_id,candidate_requirement_id) values($value,$ide)");
                    }
                }
                if ($flag == 0) {
                    $this->CandidateRequirement->updateAll(array('CandidateRequirement.calificacion' => 0), array('CandidateRequirement.id' => $ide));
                }



                $this->Session->setFlash('Registro guardado exitosamente');

                $this->setAction('index', $idr);
            }
        }
    }

    function loadRequirements($id) {
        $this->CandidateRequirement->Candidate->recursive = -1;
        $type = $this->CandidateRequirement->Candidate->find('first', array('conditions' => array('Candidate.id' => $id), 'fields' => array('Candidate.id', 'Candidate.nro_ident', 'Candidate.1er_nombre', 'Candidate.1er_apellido', 'Candidate.tipo')));
        $ide = trim($type['Candidate']['nro_ident']);
        $nom = trim($type['Candidate']['1er_nombre']);
        $apel = trim($type['Candidate']['1er_apellido']);
        $this->set('nombre', $type['Candidate']['1er_nombre'] . " " . $type['Candidate']['1er_apellido']);
        $tp = $type['Candidate']['tipo'];
        $call_id = $this->Session->read('call_id');
        $requerimientos = $this->CandidateRequirement->Requirement->find(
                'all', array(
            'conditions' => array('Requirement.requirement_type_id' => $tp, 'Requirement.call_id' => $call_id),
            'fields' => array('Requirement.id'),
            'recursive' => -1,
                ));
        $proyecto = $this->Session->read('proy_id');
        $fecha = $this->CandidateRequirement->Candidate->field('fecha_nacimiento', array('Candidate.id' => $tp));

        $edad = $this->edad($fecha);
        $conc_edad = 0;
        if ($edad < 16) {
            $conc_edad = 2;
        } elseif ($edad >= 16) {
            $conc_edad = 1;
        }



        $cargados = true;




        foreach ($requerimientos as $req) {


            $r = $req['Requirement']['id'];
            if ($cargados == true) {
                $cont = $this->CandidateRequirement->find('count', array('conditions' => array('CandidateRequirement.requirement_id' => $req['Requirement']['id'], 'CandidateRequirement.candidate_id' => $id)));
                if ($cont == 0) {
                    $cargados = false;
                }
            }


            if ($cargados == false) {

                $this->CandidateRequirement->query("INSERT INTO candidate_requirements (candidate_id,requirement_id,calificacion,claim_id)values($id,$r,0,0)");
            }

            if ($r == 4 || $r == 24 || $r == 25 || $r == 26 || $r == 27 || $r == 28) {
                $this->CandidateRequirement->updateAll(array('calificacion' => $conc_edad), array('CandidateRequirement.requirement_id' => $r, 'CandidateRequirement.candidate_id' => $id));
            }

            /*
              // verificacion sipod
              if ($r == 16 || $r == 22) {
              $res = $this->CandidateRequirement->query("SELECT identificacion FROM `sipod` WHERE `identificacion` = '$ide' AND  `primer_apellido`  LIKE ('%$apel%') AND `primer_nombre`  LIKE ('%$nom%')");

              if (count($res) > 0) {
              $cal = 1;
              } else {
              $cal = 2;
              }

              $this->CandidateRequirement->updateAll(array('calificacion' => $cal), array('requirement_id' => $r, 'CandidateRequirement.candidate_id' => $id));
              }//fin verificacion sipod
             * 
             */





            /*
             *
             *
             *
              //verificacion sisben

              if ($r==12||$r==36){
              $cal1=0;
              if( $res =$this->CandidateRequirement->query("SELECT * FROM `sisben2009` WHERE `documento` = '$ide' AND  `primer_apellido`  LIKE ('%$apel%') AND `primer_nombre`  LIKE ('%$nom%')")){
              foreach ($res as $re) {
              $nivel= $re['sisben2009']['niv_sis'];
              }

              if($nivel<=2){
              $cal1=1;
              }else{
              $cal1=2;
              }
              $sisben=true;


              }else{
              //Para personas en sisben version 3

              if( $res =$this->CandidateRequirement->query("SELECT * FROM `sisben3` WHERE `documento` = '$ide' AND  `primer_apellido`  LIKE ('%$apel%') AND `primer_nombre`  LIKE ('%$nom%')")and $sisben==false){
              echo"XXXxx";
              foreach ($res as $re) {
              $nivel0= $re['sisben3']['nivel_sis'];
              }
              if($nivel0==2||$nivel0==1){
              $cal1=1;
              }else{
              if($nivel0>2 or $nivel0==0){
              $cal1=2;
              }

              }
              $sisben=true;
              }
              //sisben version 2

              if( $res =$this->CandidateRequirement->query("SELECT * FROM `sisben2` WHERE `documento` = '$ide' AND  `primer_apellido`  LIKE ('%$apel%') AND `primer_nombre`  LIKE ('%$nom%')")){

              echo"ssssss";
              foreach ($res as $re) {
              $nivel1= $re['sisben2']['nivel'];
              }

              if($nivel1<=2){
              $cal1=1;
              }else{
              if($nivel1>2 or $nivel1==0){
              $cal1=2;
              }

              }
              }
              if($cal1==0){
              $cal1=2;
              }





              }
              $this->CandidateRequirement->updateAll(array('calificacion'=>$cal1),array('requirement_id'=>$r,'CandidateRequirement.candidate_id'=>$id));


              }

             */
            //fin de verificacion sisben
            //Verificacion en las bases de datos de adjudicacion de tierras
            /*
              if ($r==13||$r==53||$r==54||$r==55||$r==56||$r==57) {
              $res =$this->CandidateRequirement->query("SELECT * FROM `beneficiarios` WHERE `documento` = '$ide'");


              if(count ($res)>0) {
              $cal=2;
              }else {
              $cal=1;
              }

              $this->CandidateRequirement->updateAll(array('calificacion'=>$cal),array('CandidateRequirement.requirement_id'=>$r,'CandidateRequirement.candidate_id'=>$id));

              }//fin verificacion tierras
             */
        }
    }

    function edad($edad) {

        list($anio, $mes, $dia) = explode("-", $edad);
        $anio_dif = 2011 - $anio;
        $mes_dif = 5 - $mes;
        $dia_dif = 31 - $dia;
        if ($dia_dif < 0 || $mes_dif < 0)
            $anio_dif--;
        return $anio_dif;
    }

    function edit_correction($id) {
        $this->set('est', $this->Session->read('estado'));
        if (empty($this->data)) {
            $this->data = $this->CandidateRequirement->find('first', array('conditions' => array('CandidateRequirement.id' => $id)));
        } else {



            if ($this->CandidateRequirement->save($this->data)) {
                $this->Session->setFlash('Registro guardado exitosamente');
                $this->redirect(array('controller' => 'CorrectionLetters', 'action' => 'index'));
            }
        }
    }

    function reevaluation_index($id = null, $claim_id) {
        $this->layout = "ajax";
        $this->CandidateRequirement->Candidate->recursive = -1;
        $type = $this->CandidateRequirement->Candidate->find('first', array('conditions' => array('Candidate.id' => $id), 'fields' => array('Candidate.id', 'Candidate.nro_ident', 'Candidate.1er_nombre', 'Candidate.1er_apellido', 'Candidate.tipo')));
        $ide = trim($type['Candidate']['nro_ident']);
        $nom = trim($type['Candidate']['1er_nombre']);
        $apel = trim($type['Candidate']['1er_apellido']);
        $this->set('nombre', $type['Candidate']['1er_nombre'] . " " . $type['Candidate']['1er_apellido']);
        $tp = $type['Candidate']['tipo'];
        $requerimientos = $this->CandidateRequirement->Requirement->query("SELECT id FROM requirements Requirement WHERE Requirement.requirement_type_id=$tp");
        $proyecto = $this->Session->read('proy_id');
        $fecha = $this->CandidateRequirement->Candidate->field('fecha_nacimiento', array('Candidate.id' => $tp));
        $this->set('claim_id', $claim_id);
        $edad = $this->edad($fecha);
        $conc_edad = 0;
        if ($edad < 16) {
            $conc_edad = 2;
        } elseif ($edad >= 16) {
            $conc_edad = 1;
        }



        $cargados = true;




        foreach ($requerimientos as $req) {


            $r = $req['Requirement']['id'];
            if ($cargados == true) {
                $cont = $this->CandidateRequirement->find('count', array('conditions' => array('CandidateRequirement.claim_id' => $claim_id, 'CandidateRequirement.requirement_id' => $req['Requirement']['id'], 'CandidateRequirement.candidate_id' => $id)));
                if ($cont == 0) {
                    $cargados = false;
                }
            }


            if ($cargados == false) {

                $this->CandidateRequirement->query("INSERT INTO candidate_requirements (candidate_id,requirement_id,calificacion,claim_id)values($id,$r,0,$claim_id)");
            }

            if ($r == 4 || $r == 24 || $r == 25 || $r == 26 || $r == 27 || $r == 28) {
                $this->CandidateRequirement->updateAll(array('calificacion' => $conc_edad), array('CandidateRequirement.requirement_id' => $r, 'CandidateRequirement.candidate_id' => $id));
            }
        }
        $requerimientos = $this->CandidateRequirement->query("SELECT CandidateRequirement.`id` ,Requirement.texto, CandidateRequirement.`calificacion` FROM `candidate_requirements` CandidateRequirement LEFT JOIN requirements Requirement ON CandidateRequirement.`requirement_id` = Requirement.id WHERE CandidateRequirement.candidate_id=$id AND CandidateRequirement.claim_id=$claim_id");
        $this->set('requisitos', $requerimientos);
    }

    function edit_reevaluation($id, $claim_id) {
        $this->set('claim_id', $claim_id);
        $this->set('est', $this->Session->read('estado'));
        if (empty($this->data)) {
            App::import('Model', 'RequirementFault');
            $ava = new RequirementFault();
            $this->data = $this->CandidateRequirement->find('first', array('conditions' => array('CandidateRequirement.id' => $id), 'fields' => array('CandidateRequirement.id', 'CandidateRequirement.calificacion', 'CandidateRequirement.candidate_id', 'CandidateRequirement.requirement_id', 'Requirement.texto', 'Candidate.1er_nombre', 'Candidate.1er_apellido')));
            $ava->recursive = -1;
            $this->set('chequeados', $this->CandidateRequirement->CandidateRequirementsRequirementFault->findAllByCandidateRequirementId($id));
            $this->set('condiciones', $ava->find('all', array('conditions' => array('RequirementFault.requirement_id' => $this->data['CandidateRequirement']['requirement_id']), 'fields' => array('RequirementFault.id', 'RequirementFault.nombre', 'RequirementFault.tipo'), 'order' => array('RequirementFault.tipo ASC'))));
        } else {
            $datos = $this->params['form'];
            $this->CandidateRequirement->create();
            if ($this->CandidateRequirement->save($this->data)) {
                $ide = $this->data['CandidateRequirement']['id'];
                $idr = $this->data['CandidateRequirement']['candidate_id'];

                $this->CandidateRequirement->CandidateRequirementsRequirementFault->recursive = -1;
                $this->CandidateRequirement->CandidateRequirementsRequirementFault->deleteAll(array('CandidateRequirementsRequirementFault.candidate_requirement_id' => $ide));
                $flag = 0;
                foreach ($datos['checked'] as $value) {

                    if ($value != 0) {
                        $flag = 1;
                        $this->CandidateRequirement->CandidateRequirementsRequirementFault->query("INSERT INTO candidate_requirements_requirement_faults (requirement_fault_id,candidate_requirement_id) values($value,$ide)");
                    }
                }
                if ($flag == 0) {
                    $this->CandidateRequirement->updateAll(array('CandidateRequirement.calificacion' => 0), array('CandidateRequirement.id' => $ide));
                }



                $this->Session->setFlash('Registro guardado exitosamente');

                $this->setAction('reevaluation_index', $idr, $claim_id);
            }
        }
    }

}

?>
